#include <sourcemod>
#include <sdktools>

#define DEFAULT_TIMER_FLAGS TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE

public Plugin myinfo = 
{
	name = "BS Sucking Blood",
	author = "那一世的悲伤",
	description = "打队友吸血",
	version = SOURCEMOD_VERSION,
	url = "http://www.sourcemod.net/"
};

ConVar bsSuckingBlood;
int freezeSerial[MAXPLAYERS+1] = { 0, ... };
int freezeTime[MAXPLAYERS+1] = { 0, ... };
int serialGen = 0;

public void OnPluginStart()
{
	bsSuckingBlood = CreateConVar("bs_suckingblood", "1", "开启打队友吸血");
	RegAdminCmd("sm_suckingblood", OnSuckingBlood, ADMFLAG_CHANGEMAP, "更换游戏模式");
	HookEvent("player_death", OnPlayerDeath);
    HookEvent("player_hurt", OnPlayerHurt);
}


bool IsValidClient(int client)
{
	return (client > 0 && client <= MaxClients && IsClientInGame(client));
}

void FreezeClient(int client, int time)
{
	SetEntityMoveType(client, MOVETYPE_NONE);
	SetEntityRenderColor(client, 0, 128, 255, 192);
	freezeTime[client] = time;
	freezeSerial[client] = ++ serialGen;
	CreateTimer(1.0, Timer_Freeze, client | (serialGen << 7), DEFAULT_TIMER_FLAGS);
}

void UnfreezeClient(int client)
{
	freezeSerial[client] = 0;
	freezeTime[client] = 0;
	if (IsClientInGame(client))
	{
		SetEntityMoveType(client, MOVETYPE_WALK);
		SetEntityRenderColor(client, 255, 255, 255, 255);
	}
}

public Action Timer_Freeze(Handle timer, any value)
{
	int client = value & 0x7f;
	int serial = value >> 7;
	if (!IsClientInGame(client)|| !IsPlayerAlive(client)|| freezeSerial[client] != serial)
	{
		UnfreezeClient(client);
		return Plugin_Stop;
	}
	if (freezeTime[client] == 0)
	{
		UnfreezeClient(client);
		PrintHintText(client,"已解冻");
		return Plugin_Stop;
	}
	PrintHintText(client, "%d 秒后解冻", freezeTime[client]);
	freezeTime[client]--;
	SetEntityMoveType(client, MOVETYPE_NONE);
	SetEntityRenderColor(client, 0, 128, 255, 135);
	return Plugin_Continue;
}

public void OnPlayerDeath(Event event, const char[] name, bool dontBroadcast)
{
	if(bsSuckingBlood.IntValue!=1)
	{
		return;
	}
	int client = GetClientOfUserId(event.GetInt("userid"));
	int attacker = GetClientOfUserId(event.GetInt("attacker"));
	if(IsValidClient(client) && IsValidClient(attacker)) 
	{
		if(GetClientTeam(attacker) == GetClientTeam(client) && GetClientTeam(client) != 3 && attacker != client)
		{
			FreezeClient(attacker,50);
			PrintToChatAll("[BS]丧心病狂的\x03%N\x01黑死了\x03%N\x01,冰冻50秒",attacker,client);
		}
	}
}

public void OnPlayerHurt(Event event, const char[] name, bool dontBroadcast)
{
	if(bsSuckingBlood.IntValue==0)
	{
		return;
	}
  	int client = GetClientOfUserId(event.GetInt("userid")); //被攻击
	int attacker = GetClientOfUserId(event.GetInt("attacker")); //攻击方
	if(IsValidClient(client) && IsValidClient(attacker)) 
	{
		if(GetClientTeam(attacker) == GetClientTeam(client) && GetClientTeam(client) != 3 && attacker != client)
		{
			int attackerHealth = GetClientHealth(attacker);
			int dmgHealth= GetEventInt(event, "dmg_health");
			int suckingHealth=RoundToCeil(dmgHealth*0.7);
			SetEntProp(attacker, Prop_Send, "m_iHealth", attackerHealth + suckingHealth, 1);
		}
	}
}

public Action OnSuckingBlood(int client, int args)
{
	if(bsSuckingBlood.IntValue==1)
	{
		bsSuckingBlood.IntValue=0;
		PrintToChatAll("\x04[提示]\x05已关闭吸血")
	}
	else
	{
		bsSuckingBlood.IntValue=1;
		PrintToChatAll("\x04[提示]\x05已开启吸血")
	}
	return Plugin_Handled;
}